package com.shopping.adminServlet;

import java.io.IOException;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.shopping.bean.Goods;
import com.shopping.bean.Page;
import com.shopping.service.GoodsService;
import com.shopping.service.impl.GoodsServiceImpl;

/**
 * @author 2165435274
 *	后台获取商品数据
 *function load(){
 *	var keyword = "";
 *	$.ajxa({
 *		url:"shopping/getGoods.do",
 *		type:"GET",
 *		data:{
 *			"pageNum":1,
 *			"pageSize":10,
 *			"keyword":keyword
 *		},
 *		success:function(data){
 *			var goods = data.data;
 *			for(var i = 0;i < goods.lenght;i++){
 *				var g = goods[i];
 *				var $input1 = ("<input type='text' name='goodsId' class='goodsId'>").val(g.goodsId);
 *				var $input2 = ("<input type='text' name='gName' class='gName'>").val(g.gName);
 *				var $input3 = ("<input type='text' name='gPrice' class='gPrice'>").val(g.gPrice);
 *				var $input4 = ("<input type='text' name='gType' class='gType'>").val(g.gType);
 *				var $input5 = ("<input type='text' name='gIntro' class='gIntro'>").val(g.gIntro);
 *				var $input6 = ("<input type='text' name='gIf' class='gIf'>").val(g.gIf);
 *				var $input7 = ("<input type='text' name='busineId' class='busineId'>").val(g.busineId);
 *				var $input8 = ("<input type='botten' value='删除'>").click({
 *					$.ajax({
 *						url:"shopping/updateGoods.do",
 *						type:"GET",
 *						data:{
 *							"goodsId":g.goodsId,
 *							"keyword":"删除"
 *						},
 *						success:function(data){
 *							alert("删除成功");
 *						}
 *					});
 *				});
 *				var $input9 = ("<input type='botten' value='修改').click(function(event){
 *					
 *					$input9.val("确认").click(function(event){
 *						var gName = $(".gName").val();
 *						var gPrice = $(".gPrice").val();
 *						var gType = $(".gType").val();
 *						var gIntro = $(".gIntro").val();
 *						var gIf = $(".gIf").val();
 *						var busineId = $(".busineId").val();
 *						$.ajax({
 *							url:"shopping/updateGoods.do",
 *							type:"GET",
 *							data:{
 *								"goodsId":g.goodsId,
 *								"gName":gName,
 *								"gPrice":gPrice,
 *								"gType":gType,
 *								"gIntro":gIntro,
 *								"gIf":gIf,
 * 								"busineId":busineId
 *							},
 * 							success:function(data){
 * 								alert("更改成功");
 * 							}	
 *						});
 *					});
 *					
 *				});
 *			$(".formTbody").append($input1).append($input2).append($input3).append($input4).append($input5).append($input6).append($input7).append($input8).append($input9);
 *			}
 *		}
 *	})
 *}
 *
 */
@WebServlet("/getGoods.do")
public class AdminServletGetGoods extends HttpServlet{

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		Map<String,String[]> map = req.getParameterMap();
		
		GoodsService gs = new GoodsServiceImpl();
		
		Page<Goods> page = gs.findGoods(map);
		ObjectMapper om = new ObjectMapper();
		resp.setCharacterEncoding("UTF-8"); //设置 后端给前端传回的编码格式为utf-8
		resp.setContentType("application/json");// 后端告知前端 返回的数据 是 json格式(不是html /css)
		resp.getWriter().print(om.writeValueAsString(page));
		
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doPost(req,resp);
	}

	
}
